package cn.iocoder.an.module.mold.controller.admin.drawingno;

import cn.iocoder.an.module.mold.dal.dataobject.drawingno.DrawingNoDO;
import cn.iocoder.an.module.mold.utils.ImportRespVO;
import com.fhs.core.trans.anno.TransMethodResult;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.drawingno.vo.*;
import cn.iocoder.an.module.mold.service.drawingno.DrawingNoService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 模具图号")
@RestController
@RequestMapping("/mold/drawing-no")
@Validated
public class DrawingNoController {

    @Resource
    private DrawingNoService drawingNoService;

    @PostMapping("/create")
    @Operation(summary = "创建模具图号")
    @PreAuthorize("@ss.hasPermission('mold:drawing-no:create')")
    public CommonResult<Long> createDrawingNo(@Valid @RequestBody DrawingNoSaveReqVO createReqVO) {
        return success(drawingNoService.createDrawingNo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具图号")
    @PreAuthorize("@ss.hasPermission('mold:drawing-no:update')")
    public CommonResult<Boolean> updateDrawingNo(@Valid @RequestBody DrawingNoSaveReqVO updateReqVO) {
        drawingNoService.updateDrawingNo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具图号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:drawing-no:delete')")
    public CommonResult<Boolean> deleteDrawingNo(@RequestParam("id") Long id) {
        drawingNoService.deleteDrawingNo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具图号")
    @PreAuthorize("@ss.hasPermission('mold:drawing-no:delete')")
    public CommonResult<Boolean> deleteDrawingNoList(@RequestParam("ids") List<Long> ids) {
        drawingNoService.deleteDrawingNoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具图号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:drawing-no:query')")
    @TransMethodResult
    public CommonResult<DrawingNoRespVO> getDrawingNo(@RequestParam("id") Long id) {
        DrawingNoDO drawingNo = drawingNoService.getDrawingNo(id);
        return success(BeanUtils.toBean(drawingNo, DrawingNoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具图号分页")
    @PreAuthorize("@ss.hasPermission('mold:drawing-no:query')")
    @TransMethodResult
    public CommonResult<PageResult<DrawingNoRespVO>> getDrawingNoPage(@Valid DrawingNoPageReqVO pageReqVO) {
        PageResult<DrawingNoDO> pageResult = drawingNoService.getDrawingNoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DrawingNoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具图号 Excel")
    @PreAuthorize("@ss.hasPermission('mold:drawing-no:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDrawingNoExcel(@Valid DrawingNoPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DrawingNoDO> list = drawingNoService.getDrawingNoPage(pageReqVO).getList();
        ExcelUtils.write(response, "模具图号.xls", "数据", DrawingNoRespVO.class,
                BeanUtils.toBean(list, DrawingNoRespVO.class));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得保养设置导入模板导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<DrawingNoImportExcelVO> list = Arrays.asList(
                DrawingNoImportExcelVO.builder()
                        .drawingNo("code1")
                        .customer("小米")
                        .categoryCode("SDWG")
                        .upkeepCode("TESE")
                        .recheckCode("fujian1")
                        .lifeDeductionId(1)
                        .inventoryStatus("在库")
                        .devCode("ZH93")
                        .warehouseCode("PTCK")
                        .storageCode("123")
                        .description("")
                        .build()
        );
        // 输出
        ExcelUtils.write(response, "模具图号导入模板.xlsx", "列表", DrawingNoImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入图号")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<DrawingNoImportExcelVO> list = ExcelUtils.read(file, DrawingNoImportExcelVO.class);
        return success(drawingNoService.importDrawingNoList(list, updateSupport));
    }


    @GetMapping("/all")
    @Operation(summary = "获得所有图号分类")
    public CommonResult<List<DrawingNoDO>> getDrawingNoAll() {
        return success(drawingNoService.getDrawingNoAll());
    }

}